var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    showUpdateDialog:false,
    dialogTitle:"修改名称",
    placeholder:"请输入名称",
    operateType:"name",
    inputValue:"",
    userInfo:{},
    genderIndex:0,
    gender:['未知','男','女'],
    region: ['省', '市', '区'],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的账号', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  // 加载用户信息
  loadUserInfo(){
    var _this = this;
    var userInfo = wx.getStorageSync('userInfo')
    if(userInfo ==''){
      wx.showModal({
        title:'请授权登录！',
        content:'您还未授权登录！',
        showCancel:false,
        success(r){
          wx.reLaunch({
            url: '../../../login/index',
          })
        }
      })
    } else {
      requestClient.getUserDetailInfoById(userInfo.id,function(res){
          _this.setData({
            userInfo:res.data.data,
            genderIndex:res.data.data.gender
          });
          if(res.data.data.region){
            var region = res.data.data.region;
            _this.setData({
              region:region.split(',')
            });
          }
      });
    }
  },
  onShow(){
    this.loadUserInfo();
  },
  // 显示对话框
  bindShowUpdate(e){
    var _this = this;
    var operate = e.currentTarget.dataset.operate;
    var value = e.currentTarget.dataset.value;
    _this.setData({
      operateType:operate
    });
    if(operate == "name"){
      _this.setData({
        dialogTitle:'修改名称',
        placeholder:'请输入名称'
      });
    } else {
      _this.setData({
        dialogTitle:'修改邮箱',
        placeholder:'请输入邮箱'
      });
    }
    _this.setData({
      showUpdateDialog:true,
      inputValue:value
    });
  },
  // 输入框
  formInputChange(e){
    this.setData({
      inputValue:e.detail.value
    })
  },
  // 按钮操作
  bindUpdate:function(e){
    var _this = this;
    _this.setData({
      showUpdateDialog:false
    });
    if(e.detail.index == 1){
        if(_this.data.inputValue.trim() == ''){
          wx.showToast({
            title: '参数不能为空!',
            icon:'error',
            mask:true
          })
          return;
        }
        var operateType = _this.data.operateType;
        var userInfo = _this.data.userInfo;
        if(operateType == 'name'){
          userInfo.nickName = _this.data.inputValue;
        } else {
          userInfo.email = _this.data.inputValue;
        }
        
        _this.saveUserInfo(userInfo);
    }
  },
  // 修改性别
  bindGenderChange(e){
    var _this = this;
    _this.setData({
      genderIndex:parseInt(e.detail.value)
    });
    var userInfo = _this.data.userInfo;
    userInfo.gender = this.data.genderIndex;
    this.saveUserInfo(userInfo);
  },
  // 修改地区
  bindRegionChange(e){
    this.setData({
      region:e.detail.value
    });
    var userInfo = this.data.userInfo;
    userInfo.region = this.data.region.join(',');
    this.saveUserInfo(userInfo);
  },
  // 保存用户信息
  saveUserInfo:function(userInfo){
    var _this = this;
    requestClient.updateUserInfo(userInfo,function(res){
      _this.loadUserInfo();
    });
  }
})
